package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.DTO.SysUserDTO;
import com.itheima.DTO.SysUserPageDTO;
import com.itheima.VO.YddUserVO;
import com.itheima.entity.SysRole;
import com.itheima.entity.SysUser;
import com.itheima.json.PageBean;
import com.itheima.mapper.SysRoleMapper;
import com.itheima.mapper.SysUserMapper;
import com.itheima.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements SysUserService {

    private final SysUserMapper sysUserMapper;

    private final SysRoleMapper sysRoleMapper;

    @Override
    public PageBean<YddUserVO> page(SysUserPageDTO sysUserPageDTO) {
        PageHelper.startPage(sysUserPageDTO.getPage(), sysUserPageDTO.getLimit());
        List<SysUser> sysUserList = sysUserMapper.selectSysUserByLoginName(sysUserPageDTO);
        List<YddUserVO> yddUserVOList = listTransformToVO(sysUserList);
        PageInfo<YddUserVO> pageInfo = PageInfo.of(yddUserVOList);
        PageBean<YddUserVO> pageBean = new PageBean<>();
        pageBean.setCount(sysUserList.size());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setPageSize(sysUserPageDTO.getLimit());
        pageBean.setCurrentPage(sysUserPageDTO.getPage());
        pageBean.setData(pageInfo.getList());
        return pageBean;
    }

    @Override
    public void insertSysUser(SysUserDTO sysUserDTO) {

        SysUser sysUser = new SysUser();
        sysUser.setId(UUID.randomUUID().toString());
        sysUser.setLoginName(sysUserDTO.getLogin_name());
        sysUser.setPassword(DigestUtils.md5DigestAsHex(sysUserDTO.getPassword().getBytes()));
        sysUser.setName(sysUserDTO.getName());
        sysUser.setEmail(sysUserDTO.getEmail());
        sysUser.setPhone(sysUserDTO.getPhone());
//        sysUser.setLoginIp();
        sysUser.setLoginDate(LocalDateTime.now());
        sysUser.setCreateDate(LocalDateTime.now());
        sysUser.setUpdateDate(LocalDateTime.now());
        sysUser.setDelFlag("0");
        sysUser.setRoleId(JSON.toJSONString(sysUserDTO.getRole_id()));
        sysUser.setStatus(0);
//        sysUser.setDefaultData();

        sysUserMapper.insertSysUser(sysUser);
    }

    @Override
    public YddUserVO selectUserById(String id) {
        SysUser sysUser = sysUserMapper.selectUserById(id);
        YddUserVO yddUserVO = new YddUserVO();
        BeanUtils.copyProperties(sysUser, yddUserVO);
        yddUserVO.setLogin_name(sysUser.getLoginName());
        yddUserVO.setLogin_ip(sysUser.getLoginIp());
        yddUserVO.setLogin_date(sysUser.getLoginDate());
        yddUserVO.setCreate_date(sysUser.getCreateDate());
        yddUserVO.setUpdate_date(sysUser.getUpdateDate());
        yddUserVO.setDel_flag(sysUser.getDelFlag());
        yddUserVO.setRole_id(JSON.parseArray(sysUser.getRoleId(), String.class));
        yddUserVO.setDefault_data(sysUser.getDefaultData());

        return yddUserVO;
    }

    @Override
    public void deleteSysUserById(String id) {
        sysUserMapper.deleteSysUserById(id);
    }

    @Override
    public void updateSysUser(SysUserDTO sysUserDTO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDTO, sysUser);
        if (sysUserDTO.getStatus() == 2) {
            sysUser.setStatus(0);
        }
        sysUser.setLoginName(sysUserDTO.getLogin_name());
        if (sysUserDTO.getPassword() != null) {
            sysUser.setPassword(DigestUtils.md5DigestAsHex(sysUserDTO.getPassword().getBytes()));
        }
        sysUser.setRoleId(JSONObject.toJSONString(sysUserDTO.getRole_id()));

        sysUserMapper.updateSysUser(sysUser);
    }

    private List<YddUserVO> listTransformToVO(List<SysUser> sysUserList) {
        return sysUserList.stream().map(user -> {
            //反序列化
            List<String> roleIdList = JSON.parseArray(user.getRoleId(), String.class);
            YddUserVO yddUserVO = new YddUserVO();
            yddUserVO.setId(user.getId());
            yddUserVO.setLogin_name(user.getLoginName());
            yddUserVO.setPassword(user.getPassword());
            yddUserVO.setName(user.getName());
            yddUserVO.setEmail(user.getEmail());
            yddUserVO.setPhone(user.getPhone());
            yddUserVO.setLogin_ip(user.getLoginIp());
            yddUserVO.setLogin_date(user.getLoginDate());
            yddUserVO.setCreate_date(user.getCreateDate());
            yddUserVO.setUpdate_date(user.getUpdateDate());
            yddUserVO.setDel_flag(user.getDelFlag());
            yddUserVO.setRole_id(roleIdList);
            yddUserVO.setStatus(user.getStatus());
            yddUserVO.setDefault_data(user.getDefaultData());
            List<String> roleName = new ArrayList<>();
            roleIdList.forEach(roleId -> {
                SysRole sysRole = sysRoleMapper.selectRoleById(roleId);
                roleName.add(sysRole.getName());
            });
            yddUserVO.setRolename(roleName);
            return yddUserVO;
        }).collect(Collectors.toList());
    }
}
